package com.itHsuSE.experiment;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Experiment3_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩：");
        int score;
        try {
            score = scanner.nextInt(); // 读取用户输入的整数成绩
            if (score < 0) {
                throw new NegativeScoreException(score); // 抛出负分异常
            } else if (score > 100) {
                throw new ScoreOutOfRangeException(score); // 抛出超过范围异常
            } else {
                System.out.println("成绩录入成功~"); // 成绩在合理范围内
            }
        } catch (InputMismatchException e) {
            System.out.println("输入的数据类型错误~"); // 用户输入的数据类型错误
        } catch (NegativeScoreException e) {
            System.out.println(e); // 输出负分异常信息
        } catch (ScoreOutOfRangeException e) {
            System.out.println(e); // 输出超过范围异常信息
        }
    }
}

// 自定义异常类，表示成绩超过合理范围
class ScoreOutOfRangeException extends Exception {
    private int score;

    public ScoreOutOfRangeException(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "成绩 " + score + " ，超过了合理范围[0,100]~";
    }
}

// 自定义异常类，表示成绩为负数
class NegativeScoreException extends Exception {
    private int score;

    public NegativeScoreException(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "成绩 " + score + " ，不能为负数~";
    }
}